import os, sys
print "here"

    
    
    

import marshal, struct, array


from itertools import izip
import time
t = tuple(izip(range(20),  range(5, 50, 2)))

def marshal_save (t):
    start = time.time()
    cache_file = open("delete.me.cache", "w")
    for i in range(100000):
        marshal.dump(t, cache_file)
    end = time.time()
    print end - start

def marshal_load ():
    start = time.time()
    cache_file = open("delete.me.cache")
    for i in range(100000):
        new_t = marshal.load(cache_file)
    end = time.time()
    print end - start
    return new_t

def file_save (t):
    start = time.time()
    cache_file = open("delete.me.cache", "wb")
    for i in range(100000):
        cache_file.write(struct.pack("l", len(t)))
        d = [tt[0] for tt in t]
        cache_file.write(struct.pack("l" * len(t), *d ))
        d = [tt[1] for tt in t]
        cache_file.write(struct.pack("l" * len(t), *d ))
    end = time.time()
    print end - start

def array_save (t):
    start = time.time()
    cache_file = open("delete.me.cache", "wb")
    for i in range(100000):
        cache_file.write(struct.pack("l", len(t)))
        d = [tt[0] for tt in t]
        cache_file.write(struct.pack("l" * len(t), *d ))
        d = [tt[1] for tt in t]
        cache_file.write(struct.pack("l" * len(t), *d ))
    end = time.time()
    print end - start

def array_load ():
    start = time.time()
    cache_file = open("delete.me.cache", "rb")
    for i in range(100000):
        len_t = int(struct.unpack("l", cache_file.read(4))[0])
        d1 = array.array('l', cache_file.read(4 * len_t))
        d2 = array.array('l', cache_file.read(4 * len_t))
        new_t = tuple(izip(d1, d2))
    end = time.time()
    print end - start
    return new_t

def file_load ():
    start = time.time()
    cache_file = open("delete.me.cache", "rb")
    for i in range(100000):

        len_t = int(struct.unpack("l", cache_file.read(4))[0])
        d1 = struct.unpack("l"*len_t, cache_file.read(4 * len_t))
        d2 = struct.unpack("l"*len_t, cache_file.read(4 * len_t))
        new_t = tuple(izip(d1, d2))
    end = time.time()
    print end - start
    return new_t

    
def test ():
    marshal_save(t)
    new_t = marshal_load()
    assert (t == new_t)
    
    file_save(t)
    new_t = file_load()
    assert (t == new_t)

    new_t = array_load()
    assert (t == new_t)
    
test()
